#include "pch.h"
#include "HelloDWrite.h"
#include "Helper.h"
#include "Factories.h"

#include <ppltasks.h>

using namespace concurrency;
using namespace DXBase;
using namespace DXBase::Samples;
using namespace DXBase::D3D;
using namespace Windows::Foundation;

using namespace Microsoft::WRL;

void HelloDWrite::Reset()
{
}
IAsyncOperation<bool>^ HelloDWrite::LoadAsync(DXContext^ ctxt)
{
	HR2Ex(ctxt->m_d2dContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &m_blackBrush));
	return ContextWatcher::OperationSuccess;
}
void HelloDWrite::Resize(DXContext^ ctxt)
{
	ComPtr<IDWriteTextFormat>                       m_textFormat;
	ComPtr<IDWriteTypography>                       m_textTypography;

	// Create a DirectWrite text format object.
	HR2Ex(Factories::GetDWriteFactory()->CreateTextFormat(
			L"Gabriola",
			nullptr,
			DWRITE_FONT_WEIGHT_REGULAR,
			DWRITE_FONT_STYLE_NORMAL,
			DWRITE_FONT_STRETCH_NORMAL,
			64.0f,
			L"en-US", // locale
			&m_textFormat
			));
	HR2Ex(m_textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER));
	HR2Ex(m_textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER));

    Platform::String^ text = "Hello World From ... DirectWrite!";
    D2D1_SIZE_F size = ctxt->m_d2dContext->GetSize();

    // Create a DirectWrite Text Layout object
	HR2Ex(Factories::GetDWriteFactory()->CreateTextLayout(
            text->Data(),                       // Text to be displayed
            text->Length(),                     // Length of the text
            m_textFormat.Get(),                 // DirectWrite Text Format object
            size.width,                         // Width of the Text Layout
            size.height,                        // Height of the Text Layout
            &m_textLayout
            )
        );

    // Text range for the "DirectWrite!" at the end of the string
    DWRITE_TEXT_RANGE textRange = {21, 12}; // 21 references the "D" in DirectWrite! and 12 is the number of characters in the word

    // Set the font size on that text range to 100
    // Create a DirectWrite Typography object
    HR2Ex(m_textLayout->SetFontSize(100.0f, textRange));
    HR2Ex(Factories::GetDWriteFactory()->CreateTypography(&m_textTypography));

    // Enumerate a stylistic set 6 font feature for application to our text layout
    // Apply the previously enumerated font feature to our Text Typography object
    DWRITE_FONT_FEATURE fontFeature = {DWRITE_FONT_FEATURE_TAG_STYLISTIC_SET_6, 1};
    HR2Ex(m_textTypography->AddFontFeature(fontFeature));

    // Move our text range to the entire length of the string
    // Apply our recently defined typography to our entire text range
    textRange.length = text->Length();
    textRange.startPosition = 0;
    HR2Ex(m_textLayout->SetTypography(m_textTypography.Get(), textRange));

    // Move the text range to the end again
    // Set the underline on the text range
    // Set the font weight on the text range
    textRange.length = 12;
    textRange.startPosition = 21;
    HR2Ex(m_textLayout->SetUnderline(TRUE, textRange));
    HR2Ex(m_textLayout->SetFontWeight(DWRITE_FONT_WEIGHT_BOLD, textRange));
}
void HelloDWrite::Render(DXContext^ ctxt, PerformanceTimer^ timer)
{
	//D2D1_POINT_2F p0 = {0,0};
	//for (int i=0; i<10; i++)
	//{
	//	D2D1_POINT_2F p1 = {i * 30, 300};
	//	ctxt->m_d2dContext->DrawLine(p0, p1, m_blackBrush.Get(), 1, NULL);
	//	D2D1_POINT_2F p2 = {300, i*30};
	//	ctxt->m_d2dContext->DrawLine(p0, p2, m_blackBrush.Get(), 1, NULL);
	//}
    ctxt->m_d2dContext->DrawTextLayout(D2D1::Point2F(0.0f, 0.0f), m_textLayout.Get(), m_blackBrush.Get());
}
